﻿using StockMarketTool.Model;
using StockMarketTool.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StockMarketTool
{
    /// <summary>
    /// AddWin.xaml 的交互逻辑
    /// </summary>
    public partial class AddWin : Window
    {
        public AddWin(MainViewModel vm)
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        private void Exit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
        {
            string keyword = this.tbCode.Text.ToLower().Trim();
            StockModel stock = e.Item as StockModel;
            if (stock != null)
            {
                if (string.IsNullOrEmpty(keyword))
                {
                    e.Accepted = false;
                }
                else
                {
                    //有关键字、筛选名称或编码
                    e.Accepted = stock.name.Contains(keyword) || stock.symbol.Contains(keyword) || stock.code.Contains(keyword);
                }
            }
        }
        private void SetStock(object sender)
        {
            StockModel model = lb.SelectedItem as StockModel;
            if (model == null)
                return;
            this.tbCode.Text = model.symbol;
        }
        private void ListBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            SetStock(sender);
        }
        
        private void TbCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.tbCode.Text))
            {
                this.popStockList.IsOpen = false;
            }
            else
            {
                this.popStockList.IsOpen = true;
            }
            CollectionViewSource StockSource = (CollectionViewSource)this.FindResource("StockSource");
            StockSource.View.Refresh();//刷新View
        }

        private void TbCode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                int idx = lb.SelectedIndex;
                if (idx == -1)
                {
                    lb.SelectedItem = lb.Items[lb.Items.Count - 1];
                }
                else
                {
                    if (idx == 0)
                    {
                        lb.SelectedItem = lb.Items[lb.Items.Count - 1];
                        idx = lb.Items.Count;
                    }
                    lb.SelectedItem = lb.Items[idx - 1];
                }
            }
            else if (e.Key == Key.Down)
            {
                int idx = lb.SelectedIndex;
                if (idx == -1)
                {
                    lb.SelectedItem = lb.Items[0];
                }
                else
                {
                    if (idx == lb.Items.Count - 1)
                    {
                        lb.SelectedItem = lb.Items[0];
                    }
                    else
                    {
                        lb.SelectedItem = lb.Items[idx + 1];
                    }
                }
                idx = lb.SelectedIndex;
            }
            else if (e.Key == Key.Enter)
            {
                SetStock(sender);
                this.btnOk.Focus();
            }
        }
    }
}
